package kcms.user.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import kcms.common.action.CommonAction;
import kcms.common.service.CommonService;
import kcms.db.User;
import kcms.user.dao.UserDAO;
import kcms.user.service.UserService;

@Controller("UserAction")
@Scope("prototype")
public class UserAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	@Resource
	private UserService userservice;
	@Resource
	private CommonAction commonAction;
	@Resource
	private CommonService commonService;
		/*private List<User> userlist;
	
	public List<User> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<User> userlist) {
		this.userlist = userlist;
	}
*/
	//private Logger log=Logger.getLogger(this.getClass());
	/*private String id;
	private String password;*/
	private User user;
	private User usernow;
  /*public String  login() {
	  //user=new User();
	  user.setGender(true);
	  user.setId("zhuzhu");
	  user.setPassword("999");
	  user.setPhone("999");
	  user.setRealname("猪猪");
	  user.setRole("student");
	  userservice.addUser(user);
      return "success";
  }*/
	public String login(){
		/*userlist=this.userservice.getAllUser();
		return "loginsuccess";*/
		
    	User user2=this.userservice.getUser(user.getId());
    	this.clearErrorsAndMessages();
    	if(user2==null){
    		this.addActionMessage("用户名不存在！");
    		return "loginfail";
    	}
    	else if(!user2.isIsvalid()){
    		this.addActionMessage("账户未激活或已被冻结！");
    		return "loginfail";}
    	else if (user2.getPassword().equals(user.getPassword())){
    		this.addActionMessage("登陆成功！");
    		ActionContext.getContext().getSession().put("user", user2);
    		return "loginsuccess";
    	}
    	else{
    		this.addActionMessage("登陆密码错误！");
    		return "loginfail";
    	}
    }
	
	public String register(){
		User user2=this.userservice.getUser(user.getId());
		//get duplicate user if exist
		this.clearErrorsAndMessages();
		if(user2==null){
			if(this.userservice.addUser(user)){
				this.addActionMessage("用户注册成功！请检查邮箱接收激活邮件");
				String token=commonAction.sha1(user.getId()+"saltblabla"+new Date().toString());
				commonService.saveToken(token,user.getId());
				
				commonAction.sendMail("KCMS用户注册邮件", user.getEmail(), "please visit this webpage within 48hrs \n http://www.kcms.com/kcms/common_verify?token="+token);
				return "regsuccess";
			}
			else{
				this.addActionMessage("用户保存失败，请联系管理员！");
				return "regfail";
			}
			
		}
		else{
			this.addActionMessage("用户名已存在！");
			return "regfail";
		}
		
	}
	
	public String modifyUser(){
		//usernow=(User) ActionContext.getContext().getSession().get("user");
		usernow=userservice.getUser(((User) ActionContext.getContext().getSession().get("user")).getId());
		return "modify";
	}
	public String updateUser(){
		this.userservice.updateUser(usernow);
		return "loginsuccess";
	}
	public String loginpage(){
		return "loginpage";
	}
	public String regpage(){
		return "regpage";
	}
	
	public UserService getUserservice() {
		return userservice;
	}
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUsernow() {
		return usernow;
	}

	public void setUsernow(User usernow) {
		this.usernow = usernow;
	}

	public CommonAction getCommonAction() {
		return commonAction;
	}

	public void setCommonAction(CommonAction commonAction) {
		this.commonAction = commonAction;
	}

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

/*	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}*/
  
  
}